/**
Author ffpan
Date 2010-04-04
Version 1.0
**/
$(document).ready(function(){
	//初始化中介的tips
	initTips($("#agent_name"));

	$("#sagentbtn").bind("click" , function(){
		var key =($("#agent_name").val() == $("#agent_name").attr("tips"))?"":$("#agent_name").val();
		ajaxResult(key);
	});

	$("#agent_order").bind("change" , function(){
		var key =$("#key").val(); 
		ajaxResult(key);
	});

	$("#prepage").bind("click" , function(){
		if($("#pagenow").val()=="1"){return;}
		var key =$("#key").val(); 
		var page = parseInt($("#pagenow").val());
		 $("#pagenow").val(page-1);
		 $("#pn").html(page-1);
		ajaxResult(key);
	});

	
	$("#nextpage").bind("click" , function(){
		if($("#pagenow").val()==$("#pagetotal").val()){return;}
		var key =$("#key").val(); 
		var page = parseInt($("#pagenow").val());
		$("#pagenow").val(page+1);
		$("#pn").html(page+1);
		ajaxResult(key);
	});
	

	function ajaxResult(key){
		var opt =$("#agent_order").val();
		var page = $("#pagenow").val();
		$.ajax({
			type:"GET",
			url:"ajax_agent_order.php",
			data:"type="+opt+"&key="+key+"&page="+page,
			success:function(json){
				var str = "";
				var json = eval(json);
				if(json.length==0){
					str="<p>对不起，没有找到您要的结果。</p>";
				}else{
					$.each(json, function(k){
						str += "<li><a href='agent_detail.php?aid="+json[k]["id"]+" target='blank'><img src='_skins/tplimgs/noavatar_small.gif' /></a><a href='agent_detail.php?aid='"+json[k]["id"]+"' target='blank' style='color:#000;text-decoration:underline;'><span>"+json[k]["name"]+"</span></a><img src='_skins/tplimgs/alpha.png' class='starbg star"+json[k]["stars"]+"' /></li>";
					});
				}
				$("#alistdiv").html(str);
			}
		});
	}


	//是否需要评分
	/*if($("#givestars")){
		var options	= {
			max:5,
			url	: 'ajax_stars_post.php?aid='+$("#agentid").val(),
			after_ajax: function(ret) {
				if(ret=="success"){
					alert("感谢您的评分!");
					this.enabled= false;
					$('#demo10').rater(this);
					$("#alreadystar").css("display" , "");
					$('#givestars').css("display" , "none");
				}
			}
		}
		$('#givestars').rater(options);
	}*/

	
	$("a#inline").fancybox();
	$("#cancelmsg").bind("click" , function(){
		$.fancybox.close();
	});

	$("#sendmsg").bind("click" , function(){
		var data = {
			title : $("#title").val(), 
			content : $("#content").val(),
			receiver:$("#receiver").val(),
			action:"add"
		}
		var url = "sendmsg_post.php";
		$.get(url , data, function(txt){
			if(txt=="success"){
				$("#msgsuccess").css("display" , "");
				$("#msgform").css("display" , "none");
				setTimeout(function(){
					$.fancybox.close();
				} , 1000);
				setTimeout(function(){
					$("#msgsuccess").css("display" , "none");
					$("#msgform").css("display" , "");
				} , 2000)
				
			}
		}); 
	});
	
	
	//更多条件筛选
	if($("#radiodiv2")){
		initTips($("#infoid"));
		var gCondition = {};
		$("#radiodiv2 div.multi_select").each(function(){
			$(this).bind("click" , function(e){
				var el = e.target;
				//var as = $(this).find("a");
				//as.each(function(){$(this).removeClass("selected");});
				if(el.tagName=="A"){
					var el = $(el);
					var refer = $("#"+el.attr("refer"));
					var value = el.attr("tag");
					refer.val(value);
					submitForm(false);
				}
			});
		});
		
		var as = $("#multi_select a");

		$("input[mod='hidvalue']").each(function(){
			var refer = $(this).attr("name");
			var value = $(this).val();
			for(var i=0,l=as.length;i<l;i++){
				var a = $(as[i]);
				if(a.attr("tag") == value && a.attr("refer")==refer){
					a.addClass("selected");
				}
			}
		});
		
		$("#sinfoidbtn").bind("click" , function(e){
			submitForm(true);
		});
	}
	
	function submitForm(clearpage){
		if(clearpage){$("#s_page").val(1);}
		if($("#infoid").val()==$("#infoid").attr("tips")){$("#infoid").val("");}
		$("#listform").submit();
	}
	
	if($("#pagebar")){
		if($("#pagetotal").val()==0){$("#pagebar").css("display" , "none");}
		if($("#s_page").val()==1){$("#prepage").css("visibility" , "hidden");}else{$("#prepage").css("visibility" , "visible");}
		if($("#s_page").val()==$("#pagetotal").val()){$("#nextpage").css("visibility" , "hidden");}else{$("#nextpage").css("visibility" , "visible");}

		$("#prepage").bind("click" , function(){
			if($("#s_page").val()=="1"){return;}
			var page = parseInt($("#s_page").val());
			 $("#s_page").val(page-1);
			 $("#pn").html(page-1);
			 submitForm(false);
		});

		
		$("#nextpage").bind("click" , function(){
			if($("#s_page").val()==$("#pagetotal").val()){return;}
			var page = parseInt($("#s_page").val());
			$("#s_page").val(page+1);
			$("#pn").html(page+1);
			submitForm(false);
		});
	}
	
	


}); 





